﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TruyenCuoi.Resources;
using TruyenCuoi.ViewModel;
using TruyenCuoi.Entity;
using System.Windows.Media;
using Microsoft.Phone.Tasks;
using System.IO.IsolatedStorage;
using System.Net.NetworkInformation;
using Newtonsoft.Json;

namespace TruyenCuoi
{
    public partial class MainPage : PhoneApplicationPage
    {
        MainViewModel viewModel = new MainViewModel();
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            AppName.Text = "Truyện cười";
            KindOf.Text = "Thể loại";
            DataContext = viewModel;
            var isLightTheme = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];
            var imageBrush = new ImageBrush();
            var imageUri = new Uri(isLightTheme == Visibility.Visible ? "/Assets/480x800.jpg" : "/Assets/BackGroundPage.png", UriKind.Relative);
            imageBrush.ImageSource = new System.Windows.Media.Imaging.BitmapImage(imageUri);
            LayoutRoot.Background = imageBrush;
            this.Loaded+=MainPage_Loaded;
        }

        private void ItemSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var send = (sender as ListBox);
            var lbx = (send.SelectedItem as ReturnData);
            if (lbx == null) return;
            ListStoryViewModel.IDStory = lbx.TypeStory.ToString();
            ListStoryViewModel.UrlImage = lbx.UrlImage;
            ListStoryViewModel.TitlePage = lbx.StoryName;
            send.SelectedIndex = -1;
            NavigationService.Navigate(new Uri("/ListStoryPage.xaml", UriKind.Relative));
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                WebClient wb = new WebClient();
                wb.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webdownload_complete);
                wb.DownloadStringAsync(new Uri("http://www.wservice.somee.com/appinfo/GetListAppInfoWithBannerId/Banner004", UriKind.Absolute));
            }
            else MessageBox.Show("Please connect Wifi", "Wifi", MessageBoxButton.OK);
        }
        AdvEntity j;
        private void webdownload_complete(object sender, DownloadStringCompletedEventArgs e)
        {

            if (e.Error != null)
            {
                MessageBox.Show("Some thing went wrong", "Error", MessageBoxButton.OK);
            }
            else
            {
                j = JsonConvert.DeserializeObject<AdvEntity>(e.Result);
                lstAdv.ItemsSource = j.returnData;
            }
        }
        private void lst_AppChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = lstAdv.SelectedItem as ReturnDataAd;
            WebBrowserTask webBrowserTask = new WebBrowserTask();
            webBrowserTask.Uri = new Uri(item.PublishURL, UriKind.Absolute);
            webBrowserTask.Show();
        }
    }
}